﻿// <copyright file="QldStateResultsByElectorate2006FileDefinition.cs" company="QUT">
//     Copyright (c) Ninety Nine Percent. All rights reserved.
// </copyright>

using System;
using System.IO;
using FileHelpers;
using FileHelpers.DataLink;

namespace Qut.GenerateKml.Data.FileDefinitions
{
    /// <summary>
    /// Represents the structure of the Qld_State Results by Electorate-2006.xls data file.
    /// </summary>
    [DelimitedRecord("|")]
    public class QldStateResultsByElectorate2006FileDefinition
    {
        /// <summary>
        /// Gets or sets the Number
        /// </summary>
        public string No { get; set; }

        /// <summary>
        /// Gets or sets the District Name
        /// </summary>
        public string DistrictName { get; set; }

        /// <summary>
        /// Gets or sets the Booths In
        /// </summary>
        public string BoothsIn { get; set; }

        /// <summary>
        /// Gets or sets the Candidate 1
        /// </summary>
        public string Candidate1 { get; set; }

        /// <summary>
        /// Gets or sets the Party 1
        /// </summary>
        public string Party1 { get; set; }

        /// <summary>
        /// Gets or sets the Votes 1
        /// </summary>
        public string Votes1 { get; set; }

        /// <summary>
        /// Gets or sets the Percentage 1
        /// </summary>
        public string Percentage1 { get; set; }

        /// <summary>
        /// Gets or sets the Ignore this column.
        /// </summary>
        public string Empty1 { get; set; }

        /// <summary>
        /// Gets or sets the Candidate 2
        /// </summary>
        public string Candidate2 { get; set; }

        /// <summary>
        /// Gets or sets the Party 2
        /// </summary>
        public string Party2 { get; set; }

        /// <summary>
        /// Gets or sets the Votes 2
        /// </summary>
        public string Votes2 { get; set; }

        /// <summary>
        /// Gets or sets the Percentage 2
        /// </summary>
        public string Percentage2 { get; set; }

        /// <summary>
        /// Loads the specified data file from the file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="db">The data context.</param>
        public static void Load(string filePath, SociologicalDataClassesDataContext db)
        {
            var provider = new ExcelStorage(typeof(QldStateResultsByElectorate2006FileDefinition), filePath, 4, 1);
            var results = provider.ExtractRecords() as QldStateResultsByElectorate2006FileDefinition[];
            db.TruncateQldStateResultsByElectorate2006();
            foreach (var item in results)
            {
                if (item.BoothsIn == "Incorrect Candidate Selected.")
                {
                    continue;
                }

                var record = new QldStateResultsByElectorate2006();
                record.BoothsIn = item.BoothsIn;
                record.Candidate1 = item.Candidate1;
                record.Candidate2 = item.Candidate2;
                record.DistrictName = item.DistrictName;
                record.No = item.No;
                record.Party1 = item.Party1;
                record.Party2 = item.Party2;
                record.Percentage1 = item.Percentage1;
                record.Percentage2 = item.Percentage2;
                record.Votes1 = item.Votes1;
                record.Votes2 = item.Votes2;
                db.QldStateResultsByElectorate2006s.InsertOnSubmit(record);
            }

            db.SubmitChanges();
        }
    }
}
